﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ECommerce.Library.DataStructure;
using AutoGear.BLL;

public partial class admin_sales_report : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindOrders(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindOrders(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<Product> data = null;
        if (this._useSearch)
        {
            Session["fromdate"] = null;
            Session["todate"] = null;
            DateTime fromdate = DateTime.Now;
            DateTime todate = DateTime.Now;

            if (txtDateFrom.SelectedDate > DateTime.MinValue)
            {
                fromdate = Convert.ToDateTime(ECommerce.Library.ECommerce.Utility.FormatDateTime(txtDateFrom.SelectedDate, '-'));
            }
            if (txtDateTo.SelectedDate > DateTime.MinValue)
            {
                todate = Convert.ToDateTime(ECommerce.Library.ECommerce.Utility.FormatDateTime(txtDateTo.SelectedDate, '-'));
            }
            data = Product.GetBackendProductPurchaseProductListBySoldDate(fromdate, todate);
            if (data != null)
            {
                if (data.Count > 0)
                {
                    Session["fromdate"] = fromdate;
                    Session["todate"] = todate;
                }
            this._useSearch = false;
            Store1.DataSource = data;
            Store1.DataBind();
            }
        }
      
    }
    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchOrder()
    {
        _useSearch = true;
        Store1.DataBind();
    }
    [DirectMethod(Namespace = "Polana.WebService")]
    public void ExportOrders()
    {
        Session["report"] = "Sales";
        Response.Redirect("~/admin/DownloadCSV.aspx");
    }

}